layui.define(['table', 'jquery', 'layer', 'laydate'], function (exports) {
    let layer = layui.layer;
    let $ = layui.jquery;
    let laydate = layui.laydate;

    // 工具类
    var layuiFastHelper = {
        link: function (buttons) {
            var rows = [];
            for (const i in buttons) {
                var button = buttons[i];
                if (button.child && button.child.length > 0) {
                    button.extend = (button.extend ? button.extend : '') + " data-more-buttons='" + JSON.stringify(button.child) + "'";
                }

                var tips = button.tips || button.title || '';
                if (button.text !== '' && button.text !== undefined) {
                    tips = '';
                }
                rows.push('<a href="javascript:;" data-url="' + (button.url || '') + '" data-params="' + (button.params || '') + '" data-tips="' + tips + '" class="' + (tips !== '' ? 'layer-tips' : '') + ' ' + (button.class || '') + '" ' + (button.layEvent ? 'lay-event="' + button.layEvent + '"' : '') + ' title="' + tips + '" ' + (button.extend || '') + ' >' +
                    (button.icon !== undefined ? '<i class="layui-icon ' + button.icon + '"></i> ' : '') +
                    (button.text !== undefined ? button.text : '') +
                    '</a>');
            }
            return rows.join('');
        },
        // 生成按钮
        button: function (buttons) {
            var rows = [];
            for (const i in buttons) {
                var button = buttons[i];
                button.class = button.class ? ' layui-btn layui-btn-sm ' + button.class : '';
                rows.push(button);
            }
            return layuiFastHelper.link(rows)
        },
        buttonGroup: function (buttons) {
            return '<div class="layui-btn-group">' + layuiFastHelper.button(buttons) + '</div>';
        },
        // 生成表头工具栏
        toolbar: function (buttons) {
            var aButtons = [];
            for (let i = 0; i < buttons.length; i++) {
                var button = buttons[i];
                var aClass = button.class !== undefined ? button.class.split(' ') : [];
                if (aClass.includes('btn-disabled') === true && aClass.includes('layui-btn-disabled') === false) {
                    aClass.push('layui-btn-disabled')
                    button.class = aClass.join(' ');
                }

                button.class = 'layui-btn-md ' + (button.class || '');
                aButtons.push(button);
            }
            return '<div><div class="layui-btn-group toolbar">' + layuiFastHelper.button(aButtons) + '</div></div>';
        },
        // 打开新弹窗
        open: function (title, url, options) {
            layer.open($.extend({
                type: 2,
                title: title,
                shade: false,
                shadeClose: true,
                area: [$(window).width() <= 768 ? '100%' : '80%', $(window).width() <= 768 ? '100%' : '80%'],
                maxmin: true, // 允许全屏最小化
                content: url,
                moveOut: true,
                zIndex: layer.zIndex,
                success: function (layero, index) {
                    //存储callback事件
                    $(layero).data("callback", this.callback);
                    //$(layero).removeClass("layui-layer-border");
                    layer.setTop(layero);
                    layuiFastHelper.layerfooter(layero, index);
                }
            }, options || {}));
        },
        layerfooter: function (layero, index, that) {
            var frame = layer.getChildFrame('html', index);
            var layerfooter = frame.find(".layer-footer");
            if (layerfooter.size() > 0) {
                $(".layui-layer-footer", layero).remove();
                var footer = $("<div />").addClass('layui-layer-btn layui-layer-btn-c layui-layer-footer');
                footer.html(layerfooter.html());
                if ($(".row", footer).size() === 0) {
                    $(">", footer).wrapAll("<div class='row'></div>");
                }
                footer.insertAfter(layero.find('.layui-layer-content'));
                //绑定事件
                footer.on("click", ".layui-btn", function () {
                    if ($(this).hasClass("btn-disabled") || $(this).parent().hasClass("btn-disabled")) {
                        return;
                    }
                    $(".layui-btn:eq(" + $(this).index() + ")", layerfooter).trigger("click");
                });

                var titHeight = layero.find('.layui-layer-title').outerHeight() || 0;
                var btnHeight = layero.find('.layui-layer-btn').outerHeight() || 0;
                //重设iframe高度
                $("iframe", layero).height(layero.height() - titHeight - btnHeight);
            }
            //修复iOS下弹出窗口的高度和iOS下iframe无法滚动的BUG
            if (/iPad|iPhone|iPod/.test(navigator.userAgent) && !window.MSStream) {
                var titHeight = layero.find('.layui-layer-title').outerHeight() || 0;
                var btnHeight = layero.find('.layui-layer-btn').outerHeight() || 0;
                $("iframe", layero).parent().css("height", layero.height() - titHeight - btnHeight);
                $("iframe", layero).css("height", "100%");
            }
        },
        close: function (data) {
            var index = parent.layer.getFrameIndex(window.name);
            var callback = parent.layui.$("#layui-layer" + index).data("callback");
            //再执行关闭
            parent.layer.close(index);
            //再调用回传函数
            if (typeof callback === 'function') {
                callback.call(undefined, data);
            }
        },
        // 确认弹窗
        confirm: function (title, callback) {
            layer.confirm(title, {
                icon: 3,
                title: '提示'
            }, function (index) {
                layer.close(index);
                callback();
            });
        },
        msg: function (msg, isError) {
            if (isError) {
                top.layer.msg(msg, {zIndex: top.layer.zIndex, icon: 2, time: 2000, offset: 'rt'});
            } else {
                top.layer.msg(msg, {zIndex: top.layer.zIndex, icon: 1, time: 1000, offset: 'rt'});
            }
        },
        http: function (type, url, data, tips) {
            let loading = layer.load();
            return $.ajax({
                url: url,
                dataType: 'json',
                data: data,
                type: type,
            }).then(function (result) {
                layer.close(loading);
                if (result.code !== 0) {
                    layuiFastHelper.msg(result.msg, true)
                }

                if (tips === false || (type.toUpperCase() === 'GET' && tips !== true)) {
                    return result;
                }
                layuiFastHelper.msg(result.msg)
                return result;
            }, function (e) {
                layer.close(loading);
                layuiFastHelper.msg('HTTP请求失败:' + e.statusText + '(' + e.status + ')', true)
            });
        },
        laydaterange: function (opts) {
            var date = new Date();
            laydate.render($.extend({}, {
                elem: ".laydaterange",
                type: "datetime",
                rangeLinked: true,
                range: true,
                shortcuts: [
                    {
                        text: "今天",
                        value: [
                            new Date(date.getFullYear(), date.getMonth(), date.getDate(), 0, 0, 0, 0),
                            new Date((new Date(date.getFullYear(), date.getMonth(), date.getDate() + 1, 0, 0, 0, 0)).getTime() - 1)
                        ],
                    },
                    {
                        text: "昨天",
                        value: [
                            new Date(date.getFullYear(), date.getMonth(), date.getDate() - 1, 0, 0, 0, 0),
                            new Date((new Date(date.getFullYear(), date.getMonth(), date.getDate(), 0, 0, 0, 0)).getTime() - 1)
                        ],
                    },
                    {
                        text: "最近7天",
                        value: function () {
                            var date = new Date();
                            return [
                                new Date(date.getFullYear(), date.getMonth(), date.getDate() - 6, 0, 0, 0, 0),
                                new Date((new Date(date.getFullYear(), date.getMonth(), date.getDate() + 1, 0, 0, 0, 0)).getTime() - 1)
                            ];
                        }()
                    },
                    {
                        text: "最近30天",
                        value: function () {
                            var date = new Date();
                            return [
                                new Date(date.getFullYear(), date.getMonth(), date.getDate() - 29, 0, 0, 0, 0),
                                new Date((new Date(date.getFullYear(), date.getMonth(), date.getDate() + 1, 0, 0, 0, 0)).getTime() - 1)
                            ];
                        }()
                    },
                    {
                        text: "本月",
                        value: function () {
                            var date = new Date();
                            return [
                                new Date(date.getFullYear(), date.getMonth(), 1, 0, 0, 0, 0),
                                new Date((new Date(date.getFullYear(), date.getMonth() + 1, 1, 0, 0, 0, 0)).getTime() - 1)
                            ];
                        }()
                    },
                    {
                        text: "上月",
                        value: function () {
                            var date = new Date();
                            return [
                                new Date(date.getFullYear(), date.getMonth() - 1, 1, 0, 0, 0, 0),
                                new Date((new Date(date.getFullYear(), date.getMonth(), 1, 0, 0, 0, 0)).getTime() - 1)
                            ];
                        }()
                    }
                ]
            }, opts));
        }
    };
    exports('layuiFastHelper', layuiFastHelper);
})